<?php
/**
 * Created by PhpStorm.
 * User: dungnt13
 * Date: 11/18/14
 * Time: 11:24 AM
 */

class GuitarTabs
{
    var $lyrics;
    var $tabs;

    function __contruct($l = null)
    {
        $this->lyrics = array();
        $this->tabs = array();
        $this->tabs_qry = "";

        if ($l) {
            $this->add_section('', $l);
        }
    }

    function utf8_strlen($s)
    {
        $c = strlen($s);
        $l = 0;
        for ($i = 0; $i < $c; ++$i)
            if ((ord($s[$i]) & 0xC0) != 0x80)
                ++$l;
        return $l;
    }

    function add_section($title, $lyrics)
    {
        $lines = explode("\n", $lyrics);
        $merge = '';
        foreach ($lines as $l) {
            $line = array();
            $buffkey = substr(trim($l), 0, 1);
            //echo $buffkey;
            if ($buffkey != "[.]") {
                $l = "[.]" . $l;
            } else {

            }
            $sections = preg_split("/[\[]+/", $l);
            //$sections = explode('[', $l);
            $regs = array();
            foreach ($sections as $s) {
                //$s = $merge.$s;
                if (preg_match('/^(.+)\](.+)$/', $s, $regs)) {
                    if (trim($regs[1]) == ".") {
                        $regs[1] = "";
                    }
                    $line[] = array('lyrics' => $regs[2], 'tab' => trim($regs[1]));

                    if (trim($regs[1] != '.')) {
                        $this->tabs[] = '<span class="tone-in-ct colorRed hocdan_chord">' . trim($regs[1]) . '</span>';
                        if (trim($regs[1])) {
                            $this->tabs_qry .= trim($regs[1]) . ",";
                        }
                        $merge = '';
                    }
                } else {
                    $merge = $s;
                }
            }
            if (count($line)) {
                $all[] = $line;
            }
        }
        $all = !empty($all) ? $all : "";
        $title = !empty($title) ? $title : "";
        $this->lyrics[] = array('title' => $title, 'content' => $all);
    }

    function print_fixed()
    {
        $ret = "";
        foreach ($this->lyrics as $lyric) {
            if ($lyric['title']) {
                $ret .= '<b>' . $lyric['title'] . '</b>';
                $ret .= "\n";
            }
            $i = 0;
            $c = count($lyric['content']);
            $ret .= '<div class="speard float-l"><pre>';
            if (!empty($lyric['content']) && count($lyric['content']) > 0) {
                foreach ($lyric['content'] as $line) {
                    foreach ($line as $sections) {
                        $spaceNum = $this->utf8_strlen($sections['lyrics']) - strlen($sections['tab']);
                        if ($spaceNum <= 0) {
                            $spaceNum = 1;
                        }
                        $ret .= '<span class="tone-in-ct colorRed hocdan_chord">' . trim($sections['tab']) . '</span>' . str_repeat(" ", $spaceNum);
                    }

                    $ret .= "\n";
                    foreach ($line as $sections) {
                        $ret .= trim($sections['lyrics']) . " ";
                    }
                    $ret .= "\n";
                    if ($i == $c / 2)
                        $ret .= '</pre></div><div class="speard float-r"><pre>';
                    $i++;
                }
            }
        }

        echo $ret . '</pre></div>';
    }

    function print_html()
    {
        foreach ($this->lyrics as $title => $lyric) {
            if ($lyric['title']) {
                $ret .= '<b>' . $lyric['title'] . '</b><br/>';
            }
            foreach ($lyric['content'] as $line) {
                $ret .= '<table><tr>';
                foreach ($line as $sections) {
                    $ret .= '<td style="font-size: 9pt;">' . $sections['tab'] . '</td>';
                }
                $ret .= '</tr><tr>';
                foreach ($line as $sections) {
                    $ret .= '<td>' . $sections['lyrics'] . '</td>';
                }
                $ret .= '</tr></table>';
            }
        }

        echo '' . $ret . '';
    }

    function print_tabs()
    {
        if ($this->tabs)
            echo implode(' &nbsp;&nbsp;', array_unique($this->tabs));
    }

    function print_str_query()
    {
        if (isset($this->tabs_qry)) {
            return substr($this->tabs_qry, 0, -1);
        }
    }

    function print_chords_code($cm = "")
    {
        if (empty($cm)) {
            if (isset($this->tabs_qry)) {
                $cm = substr($this->tabs_qry, 0, -1);
            }
        }
        $chord_request_arr = explode(",", $cm);
        $chord_names = array("C", "D", "E", "F", "G", "A", "B", "c", "d", "e", "f", "g", "a", "b");
//        $table = "chords2";
        $chord_arr = array("chord", "modf", "e2", "b", "g", "d", "a", "e");
        $condition = "";
        $request = false;
        $stt = 0;

        $chord_request_arr = array_unique($chord_request_arr);

        $condition = "";
        if (count($chord_request_arr) > 0) {
//            foreach ($chord_request_arr as $val) {
//                $ch = trim($val);
//                if (trim($val) != "") {
//                    $condition .= "|| chord='$ch' ";
//                }
//            }
//            $chord = $val;
            $request = true;
            if ($request) {
                $result = chords2::whereIn('chord', $chord_request_arr)->get();
                if (!empty($result)) {
                    if (isset($_GET["callback"])) {
                        echo $_GET["callback"] . "(";
                    }
                    //output json
                    $first = 1;
                    $k = 0;
                    $chordArr = array();
                    foreach ($result as $row) {
                        $app = array();
                        if ($row->f0) {
                            $app[0] = $row->f0;
                        };
                        if ($row->f1) {
                            $app[1] = $row->f1;
                        };
                        if ($row->f2) {
                            $app[2] = $row->f2;
                        };
                        if ($row->f3) {
                            $app[3] = $row->f3;
                        };
                        if ($row->f4) {
                            $app[4] = $row->f4;
                        };
                        if ($row->f5) {
                            $app[5] = $row->f5;
                        };

                        $chordArr[$row->chord][] = array(
                            "txt" => $row->txt,
                            "fret" => $row->fret,
                            "app" => $app
                        );
                    }
                    foreach ($chord_request_arr as $val) {
                        $chordArr[$val]["old_ch"] = $val;
                    }

                    echo json_encode($chordArr);
                } else {
                    if (isset($_GET['type']) && $_GET['type'] == "xml") {
                        header("content-type: text/xml");
                        $xml = new SimpleXMLElement("<chords>No chords found</chords>");
                        echo $xml->asXML();
                    } else {
                        if (isset($_GET["callback"])) {
                            echo $_GET["callback"] . "(";
                        }
                        echo '{ "chords": "No chords found" }';
                        if (isset($_GET["callback"])) {
                            echo ");";
                        }
                    }
                }
            } else {
                if (isset($_GET['type']) && $_GET['type'] == "xml") {
                    header("content-type: text/xml");
                    $xml = new SimpleXMLElement("<chords>None or invalid request parameters provided</chords>");
                    echo $xml->asXML();
                } else {
                    if (isset($_GET["callback"])) {
                        echo $_GET["callback"] . "(";
                    }
                    echo '{ "chords": "None or invalid request parameters provided" }';
                    if (isset($_GET["callback"])) {
                        echo ");";
                    }
                }
            }
        }
    }

    public function print_chords()
    {
        $cm = substr($this->tabs_qry, 0, -1);
        $chord_request_arr = array_unique(explode(",", $cm));
        return $chord_request_arr;
    }
} 